﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms.Control.ToDoList
{
    public partial class ToDoListInfoControl : BaseUserControl
    {
        private DataSet dsEmployeeInfo = new DataSet();
        private DBEntityToDoList todoListInfo = null;

        public ToDoListInfoControl()
        {
            InitializeComponent();
        }

        
        private void integerInput1_ValueChanged(object sender, EventArgs e)
        {
            progressBarX.Value = integerComplateRate.Value;
        }

        public void InitDateControl(DateTime initDate, DataSet dsEmployee, DBEntityToDoList todoList)
        {
            startDate.Value = initDate;
            startHour.initTime(initDate.Hour, initDate.Minute);
            endDate.Value = initDate;
            endHour.initTime(initDate.Hour, initDate.Minute);
            dsEmployeeInfo = dsEmployee;
            foreach (DataRow drEmployee in dsEmployeeInfo.Tables[0].Rows)
            {
                cbxEmployeeID.Items.Add(drEmployee[DBEntityEmployee.FieldName.ECODE].ToString()
                    + " " + drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
            }
            if (todoList != null)
            {
                //编辑状态的控件
                todoListInfo = (DBEntityToDoList)todoList.Clone();
                memoDecription.Text = todoListInfo.TODODECRIPTION;
                startDate.Value = todoListInfo.TODOSTARTTIME;
                startHour.CurrDateTime = todoListInfo.TODOSTARTTIME;
                endDate.Value = todoListInfo.TODOENDTIME;
                endHour.CurrDateTime = todoListInfo.TODOENDTIME;
                integerComplateRate.Value = todoListInfo.TODOCOMPLETIONRATE;
                memoEdit.Text = todoListInfo.TODOMEMO;

                DataRow[] drEmployees = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.EMPLOYEEID + " = " + todoListInfo.EMPLOYEEID);
                if (drEmployees.Length > 0)
                {
                    //代办人
                    string empCodeName = drEmployees[0][DBEntityEmployee.FieldName.ECODE].ToString() + " " + drEmployees[0][DBEntityEmployee.FieldName.ENAME].ToString();
                    int idx = cbxEmployeeID.Items.IndexOf(empCodeName);
                    if (idx != -1)
                    {
                        cbxEmployeeID.SelectedIndex = idx;
                    }
                }
                SortedList<string, DevComponents.Editors.ComboItem> sortCbxStae = new SortedList<string, DevComponents.Editors.ComboItem>();
                sortCbxStae.Add("T0I", cbxItemT0I);
                sortCbxStae.Add("T0N", cbxItemT0N);
                sortCbxStae.Add("T0T", cbxItemT0T);
                sortCbxStae.Add("T0V", cbxItemT0V);
                DevComponents.Editors.ComboItem cbxToDoListState = sortCbxStae[todoListInfo.TODOSTATE];
                if (cbxState != null)
                {
                    //状态
                    cbxState.SelectedItem = cbxToDoListState;
                }
                
            }
        }
        /// <summary>
        /// 获取界面的输入值
        /// </summary>
        /// <param name="todoListInfo"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool GetToDoListInfo(ref DBEntityToDoList todoListInfo, out string error)
        {
            error = string.Empty;
            DateTime startDateToDoList = DateTime.Now;
            DateTime endDateToDoList = DateTime.Now;
            try
            {
                if (memoDecription.Text.Trim().Equals(string.Empty))
                {
                    error = "请填写事项说明";
                    SetErrorControl(memoDecription, error);
                    throw new Exception(error);
                }

                startDateToDoList = new DateTime(startDate.Value.Year, startDate.Value.Month, startDate.Value.Day,
                    startHour.CurrDateTime.Hour, startHour.CurrDateTime.Minute, startHour.CurrDateTime.Second);

                endDateToDoList = new DateTime(endDate.Value.Year, endDate.Value.Month, endDate.Value.Day,
                    endHour.CurrDateTime.Hour, endHour.CurrDateTime.Minute, endHour.CurrDateTime.Second);

                if (startDateToDoList.CompareTo(endDateToDoList) > 0)
                {
                    error = "代办事项的结束时间不能够小于或者等于开始时间";
                    SetErrorControl(endDate, error);
                    SetErrorControl(endHour, error);
                    throw new Exception(error);
                }

                if (cbxEmployeeID.SelectedItem == null)
                {
                    error = "请选择代办人";
                    SetErrorControl(cbxEmployeeID, error);
                    throw new Exception(error);
                }

                if (integerComplateRate.Value < 0) {
                    error = "完成率必须大于等于0";
                    SetErrorControl(integerComplateRate, error);
                    throw new Exception(error);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            todoListInfo.TODODECRIPTION = memoDecription.Text;
            todoListInfo.TODOCOMPLETIONRATE = integerComplateRate.Value;
            DataRow[] drEmployee = dsEmployeeInfo.Tables[0].Select(DBEntityEmployee.FieldName.ECODE + "' '" + DBEntityEmployee.FieldName.ENAME + " = " 
                + cbxEmployeeID.SelectedItem.ToString());
            if(drEmployee.Length == 0){
                error = "没有找到员工信息：" + cbxEmployeeID.SelectedItem;
                return false;
            }
            todoListInfo.EMPLOYEEID = Convert.ToInt32(drEmployee[0][DBEntityEmployee.FieldName.EMPLOYEEID]);
            todoListInfo.TODOMEMO = memoEdit.Text;
            todoListInfo.TODOSTATE = DBEntityToDoList.ENUM_TODOSTATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());
            todoListInfo.TODOSTARTTIME = startDateToDoList;
            todoListInfo.TODOENDTIME = endDateToDoList;
            //TODO 检测员工是否休假

            return true;
        }

        private void startHour_Load(object sender, EventArgs e)
        {

        }
    }
}
